// JavaScript Document
//javascript

/*全局js*/

/*
* 登陆
* 在前端ajax请求中，当响应的error状态为为未登录(code == 'nologin')，则跳转到登陆页面
*/
function sys_login(code){
	if(code == 'nologin'){
		window.location.href = site_url+"/account";
		return true;
	}
	return false;
	
}

/*
 * 更新验证码
 * @imgobj  object 更新的前端节点 --通过前端js取到节点对象后传过来 如 $("#captcha_img")
 * @pre_captcha string 验证码的前缀标识 --由于不同的应用验证码作用域不同，需要不同的命名
 * 一定要注意 请求的地址
*/
function get_captcha(imgobj,pre_captcha){
	if(site_url == 'undefined'){
		site_url = '';
	}
	$.ajax({
		type:'post',
		url:site_url+'../common/comm_getcaptcha',
		data:'pre_captcha='+pre_captcha,
		dataType:'text',
		success:function (text){
			imgobj.html(text);
		},
		error:function (){
			imgobj.html('fail to set captcha');
		}
	});
}

/*
*下载文件
*/
function savepic(file) {
$.fileDownload(file)
    .done(function () { alert('File download a success!'); })
    .fail(function () { alert('File download failed!'); });
}

//修复使用jquery.validate插件中name属性相同(如name='a[]')时候的验证bug
	
$(function () {
if ($.validator) {
    //fix: when several input elements shares the same name, but has different id-ies....
    $.validator.prototype.elements = function () {

        var validator = this,
            rulesCache = {};

        // select all valid inputs inside the form (no submit or reset buttons)
        // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
        return $([]).add(this.currentForm.elements)
        .filter(":input")
        .not(":submit, :reset, :image, [disabled]")
        .not(this.settings.ignore)
        .filter(function () {
            var elementIdentification = this.id || this.name;

            !elementIdentification && validator.settings.debug && window.console && console.error("%o has no id nor name assigned", this);

            // select only the first element for each name, and only those with rules specified
            if (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))
                return false;

            rulesCache[elementIdentification] = true;
            return true;
        });
    };
	
	$.validator.messages = {
		 required: "不可以为空.",
		remote: "格式错误",
		email: "电子邮件格式错误.",
		url: "链接地址格式错误.",
		date: "日期格式错误.",
		dateISO: "日期格式错误 (ISO).",
		number: "必须输入一个数字.",
		digits: "必须输入整数.",
		creditcard: "信用卡号格式错误.",
		equalTo: "两次输入格式不相符",
		accept: "输入错误.",
		maxlength: $.validator.format("长度不可以超过 {0} 个字符."),
		minlength: $.validator.format("长度不可以低于 {0} 个字符."),
		rangelength: $.validator.format("长度必须在 {0} 和 {1} 之间."),
		range: $.validator.format("请输入一个 {0} 到 {1}之间的数字."),
		max: $.validator.format("请输入小于等于 {0} 的数字."),
		min: $.validator.format("请输入大于等于 {0} 的数字.")
	};
	
	$.validator.methods.equalTo = function (value, element, param) {
		// bind to the blur event of the target in order to revalidate whenever the target field is updated
			// TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
			var target = $(param);
			if ( this.settings.onfocusout ) {
				target.unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
					$(element).valid();
				});
			}
			
		
			//修复:当两个域都为空的时候，取消验证
			return value === target.val();
	}
}

});


$.zopo = {

	//社会化分享
	share:function(options) {
		//default options
		var d = {
			url:window.location.href,//分享的路径
			title :$('title').html(),//分享的标题
			summary :$('meta[name=description]').attr('content'),//分享的内容
			keywords :$('meta[name=keywords]').attr('content'),//分享的内容
			pic :'',
			shortUrl :false,//针对weibo关闭短连接
			data_track_clickback :true//统计分享数据
		
		

		};        
		 
		//new options 
		var o = $.extend(d,options);     
	},
	other:function(param) {          
			//  
		}
}



/*
* serviceid分享的媒体 ; 
* options:分享内容的相关配置，d{}里存在的参数项都可以传入
* 调用:zopoShare('kaixin001',{'title':'theurl','summary':'content'})
*/
function zopoShare(serviceid,options){
	jisthis_appkey = 0;
	
	//default options
	var d = {
			url:window.location.href,//分享的路径
			title :$('title').html(),//分享的标题
			summary :$('meta[name=description]').attr('content'),//分享的内容
			keywords :$('meta[name=keywords]').attr('content'),//分享的内容
			pic :'',
			shortUrl :false,//针对weibo关闭短连接
			data_track_clickback :true//统计分享数据
			
			
		  };

		//new options 
		var o = $.extend(d,options);
	window.open('http://www.jiathis.com/send/?webid='+serviceid+'&url='+o.url+'&title='+o.title+'&uid='+jisthis_appkey+"&summary="+o.summary+"&pic="+o.pic);
}


